package com.lbc.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lbc.bean.dto.KidDTO;
import com.lbc.bean.dto.KidSearchDTO;
import com.lbc.bean.dto.KidUpdateDTO;
import com.lbc.bean.entity.User;
import com.lbc.bean.pojo.Page;
import com.lbc.bean.vo.KidVO;
import com.lbc.bean.vo.SchoolInfoVO;
import com.lbc.common.resp.ResponseResult;
import com.lbc.common.utils.JwtUtils;
import com.lbc.dao.custom.CtSchoolMapper;
import com.lbc.service.KidService;
import com.lbc.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author xyl
 * @date 2021-03-25 14:50
 */
@Api(tags = "留守儿童")
@RestController
@RequestMapping("kid")
public class KidController {

    @Autowired
    private KidService kidService;
    @Autowired
    private UserService userService;
    @Autowired
    private CtSchoolMapper schoolMapper;


    @RequiresRoles(value = {"admin","school"},logical = Logical.OR)
    @ApiOperation("添加留守儿童")
    @PostMapping("add")
    public ResponseResult add(@RequestBody @Validated KidDTO kidDTO) {
        User userByEmail = userService.getUserByEmail(kidDTO.getParentEmail());
        if(ObjectUtils.isNotEmpty(userByEmail)){
            return ResponseResult.error("此邮箱已存在!");
        }
        return ResponseResult.toResponse(kidService.add(kidDTO));
    }

    @RequiresRoles(value = {"admin","school"},logical = Logical.OR)
    @ApiOperation("编辑留守儿童")
    @PutMapping("modify")
    public ResponseResult modify(@RequestBody KidUpdateDTO kidDTO) {
        if (ObjectUtils.isEmpty(kidDTO.getKidId())) {
            return ResponseResult.error("主键不能为空!");
        }
        User userByEmail = userService.getUserByEmail(kidDTO.getParentEmail());
        if(ObjectUtils.isNotEmpty(userByEmail)){
            return ResponseResult.error("此邮箱已存在!");
        }
        return ResponseResult.toResponse(kidService.modify(kidDTO));
    }

    @RequiresRoles(value = {"admin","school"},logical = Logical.OR)
    @ApiOperation("删除留守儿童")
    @DeleteMapping("/remove/{kidId}")
    public ResponseResult remove(@PathVariable("kidId") Integer kidId) {
        return ResponseResult.toResponse(kidService.remove(kidId));
    }

    @ApiOperation("页面展示列表")
    @GetMapping("/front/list")
    public ResponseResult<PageInfo<KidVO>> listFront(@Validated Page page, @RequestParam(required = false) String searchKey) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return new ResponseResult<>(new PageInfo<>(kidService.listFront(searchKey)));
    }

    @ApiOperation("后端管理员列表" +
            "管理员搜索条件都可以进行选择搜索 , ")
    @GetMapping("/back/list")
    public ResponseResult<PageInfo<KidVO>> listBack(@Validated KidSearchDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        return new ResponseResult<>(new PageInfo<>(kidService.listBack(dto)));
    }

    @ApiOperation("详情")
    @GetMapping("/{kidId}")
    public ResponseResult<KidVO> detail(@PathVariable Integer kidId) {
        return new ResponseResult<>(kidService.get(kidId));
    }

    @RequiresRoles("admin")
    @ApiOperation("根据学校id获取当前学校下的儿童")
    @GetMapping("/bySchool/{schoolId}")
    public ResponseResult<PageInfo<KidVO>> bySchoolDetail(Page page,@PathVariable Integer schoolId) {
        return new ResponseResult<>(new PageInfo<>(kidService.getBySchoolId(page,schoolId)));
    }
    @RequiresRoles("school")
    @ApiOperation("获取当前学校下的儿童")
    @GetMapping("/bySchool")
    public ResponseResult<PageInfo<KidVO>> schoolKidDetail(Page page) {
        SchoolInfoVO school = schoolMapper.selectByUserId(JwtUtils.getUserId());
        return new ResponseResult<>(new PageInfo<>(kidService.getBySchoolId(page,school.getSchoolId())));
    }

    @ApiOperation("前台详情")
    @GetMapping("/front/{kidId}")
    public ResponseResult<KidVO> frontDetail(@PathVariable Integer kidId) {
        return new ResponseResult<>(kidService.get(kidId));
    }


}
